package com.onlinelearn.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onlinelearn.base.exception.OnlineLearnException;
import com.onlinelearn.content.mapper.CourseBaseMapper;
import com.onlinelearn.content.mapper.CourseTeacherMapper;
import com.onlinelearn.content.model.po.CourseBase;
import com.onlinelearn.content.model.po.CourseTeacher;
import com.onlinelearn.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    CourseTeacherMapper mapper;

    @Autowired
    CourseBaseMapper courseBaseMapper;

    @Transactional
    @Override
    public List<CourseTeacher> getCourseTeacher(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(String.valueOf(courseId)),CourseTeacher::getCourseId,courseId);
        return mapper.selectList(lambdaQueryWrapper);
    }

    @Transactional
    @Override
    public CourseTeacher saveCourseTeacher(Long companyId,CourseTeacher courseTeacher) {
        CourseBase courseBase_u = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if(courseBase_u==null){
            OnlineLearnException.cast("课程信息不存在");
        }
        if(!companyId.equals(courseBase_u.getCompanyId())){
            OnlineLearnException.cast("本机构只新增修改本机构的课程");
        }

        int insert = mapper.insert(courseTeacher);
        if(insert < 1){
            OnlineLearnException.cast("新增失败!");
        }
        return courseTeacher;
    }

    @Override
    public CourseTeacher editCourseTeacher(Long companyId,CourseTeacher courseTeacher) {
        CourseBase courseBase_u = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if(courseBase_u==null){
            OnlineLearnException.cast("课程信息不存在");
        }
        if(!companyId.equals(courseBase_u.getCompanyId())){
            OnlineLearnException.cast("本机构只允许修改本机构的课程");
        }
        int insert = mapper.updateById(courseTeacher);
        if(insert < 1){
            OnlineLearnException.cast("修改失败!");
        }
        return courseTeacher;
    }

    @Override
    public void deleteCourseTeacher(Long companyId, Long courseId, Long id) {
        CourseBase courseBase_u = courseBaseMapper.selectById(courseId);
        if(courseBase_u==null){
            OnlineLearnException.cast("课程信息不存在");
        }
        if(!companyId.equals(courseBase_u.getCompanyId())){
            OnlineLearnException.cast("本机构只允许删除本机构的课程");
        }
        LambdaQueryWrapper<CourseTeacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(String.valueOf(courseId)),CourseTeacher::getCourseId,courseId);
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(String.valueOf(id)),CourseTeacher::getId,id);
        mapper.delete(lambdaQueryWrapper);
    }
}
